package com.demo.controller;

import com.demo.common.CreateDate;
import com.demo.common.CreateUUID;
import com.demo.common.StringUtil;
import com.demo.model.TabUsers;
import com.demo.model.TabUsersAcc;
import com.demo.model.TabUsersAccHistory;
import com.demo.service.TabUsersAccHistoryService;
import com.demo.service.TabUsersAccService;
import com.demo.service.TabUsersService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Log4j2
public class TabUsersAccController {

    @Resource
    private TabUsersAccService service;

    @Resource
    private TabUsersService users_service;

    @Resource
    private TabUsersAccHistoryService his_service;



    @RequestMapping(value = "tab_users_acc_pre_create")
    public String tab_users_acc_pre_create(String usersid,HttpServletRequest request){
        TabUsers u = users_service.findByid(usersid);
        request.setAttribute("u",u);
        return "index/tab_users_acc/create";
    }
    /**
     * 史忠杰
     * 开卡业务
     * 需要注入操作历史数据   tab_users_acc_history
     * */
    @RequestMapping(value = "tab_users_acc_create")
    public String tab_users_acc_create(String usersid,HttpServletRequest request){
        String loginname = (String) request.getSession().getAttribute("loginname");
        if(null==loginname){

        }else {
            List<TabUsersAcc> check_userid_list = service.findByuserid(usersid);
            if (check_userid_list.isEmpty()) {
                String id = CreateUUID.getuuid();

                String datetimes = CreateDate.getDateTimes();
                TabUsersAcc acc = new TabUsersAcc();
                acc.setId(id);
                acc.setUserid(usersid);
                acc.setMonnum("0");
                acc.setEm1("");
                acc.setEm2("");
                acc.setEm3("");
                acc.setEm4("");
                acc.setEm5("");
                acc.setCreatename(loginname);
                acc.setCreatetimes(datetimes);
                boolean b = service.save(acc);
                tab_users_acc_history_save(usersid,"0000","开卡","0");
                request.setAttribute("b", b);
            } else {

                String id = "";
                String userid = "";
                String monnum = "";
                String em1 = "";
                String em2 = "";
                String em3 = "";
                String em4 = "";
                String em5 = "";
                String createname = "";
                String createtimes = "";
                for (TabUsersAcc a : check_userid_list) {
                    id = a.getId();
                    userid = a.getUserid();
                    monnum = a.getMonnum();
                    em1 = a.getEm1();
                    em2 = a.getEm2();
                    em3 = a.getEm3();
                    em4 = a.getEm4();
                    em5 = a.getEm5();
                    createname = a.getCreatename();
                    createtimes = a.getCreatetimes();
                }
                TabUsersAcc acc_pre = new TabUsersAcc();
                acc_pre.setId(id);
                acc_pre.setUserid(userid);
                acc_pre.setMonnum(monnum);
                acc_pre.setEm1(em1);
                acc_pre.setEm2(em2);
                acc_pre.setEm3(em3);
                acc_pre.setEm4(em4);
                acc_pre.setEm5(em5);
                acc_pre.setCreatename(createname);
                acc_pre.setCreatetimes(createtimes);
                service.update_userid(acc_pre);
                tab_users_acc_history_save(usersid,"0000","开卡","0");
                request.setAttribute("b", "error");
            }
        }
        return "index/tab_users_acc/create";
    }

    @RequestMapping(value = "tab_users_acc_pre_up_money")
    public String tab_users_acc_pre_up_money(String usersid,String accid,HttpServletRequest request){
        TabUsers u = users_service.findByid(usersid);
        TabUsersAcc acc = service.findById(accid);
        request.setAttribute("u",u);
        request.setAttribute("acc",acc);
        return "index/tab_users_acc/up_money";
    }
    /**
     * 史忠杰
     * 充值业务
     * 需要注入操作历史数据   tab_users_acc_history
     * */
    @RequestMapping(value = "tab_users_acc_up_money")
    public String tab_users_acc_up_money(String usersid,String accid,String monnum,String pmonnum,HttpServletRequest request){
        if(StringUtil.isEmpty(monnum)){
            monnum = "0";
        }
        if(StringUtil.isEmpty(pmonnum)){
            pmonnum = "0";
        }
        double _pmonnum = Double.parseDouble(pmonnum);
        double _monnum = Double.parseDouble(monnum);
        double all_num = _pmonnum+_monnum;
        TabUsersAcc acc = new TabUsersAcc();
        acc.setUserid(usersid);
        acc.setId(accid);
        acc.setMonnum(all_num+"");
        boolean b = service.up_money(acc);
        String msg = "";
        if(b){
            tab_users_acc_history_save(usersid,accid,"充值",monnum);
            msg = "您的操作数据已经成功！数据已经成功进入到数据库并进行存储！";
        }else{
            msg = "您的操作数据失败！数据没有进入到数据库并进行存储！";
        }



        request.setAttribute("msg",msg);
        request.setAttribute("all_num",all_num);
        request.setAttribute("_pmonnum",_pmonnum);
        request.setAttribute("_monnum",_monnum);
        return "index/tab_users_acc/up_money_msg";
    }


    public void tab_users_acc_history_save(String userid,String accid,String way,String monnum){
        String id = CreateUUID.getuuid();
        String datetimes = CreateDate.getDateTimes();
        String em = "";
        TabUsersAccHistory his = new TabUsersAccHistory();
        his.setId(id);
        his.setUserid(userid);
        his.setAccid(accid);
        his.setWay(way);
        his.setWaytimes(datetimes);
        his.setEm1(em);
        his.setEm2(em);
        his.setEm3(em);
        his.setEm4(em);
        his.setEm5(em);
        his.setMonnum(monnum);
        his_service.save(his);
    }




}
